package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 插入菜品
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dto){
        log.info("新增菜品{}",dto);
        dishService.saveWithFlavor(dto);
        //清除缓存
        clearCatch("dish_"+dto.getCategoryId());
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("开启分页查询");
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("批量删除菜品{}",ids);
        dishService.deleteBatch(ids);
        //清除缓存
        clearCatch("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品信息")
    public Result<DishVO> selectDishAndFlavorById(@PathVariable Long id){
        DishVO dishVO = dishService.selectDishAndFlavorById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result update(@RequestBody DishDTO dto){
        log.info("修改菜品信息{}",dto);
        dishService.updateDishAndFlavor(dto);
        //清除缓存
        clearCatch("dish_*");
        return Result.success();
    }

    /**
     * 修改商品起售停售状态
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改商品起售停售状态")
    public Result updateStatus(@PathVariable Integer status,Long id){
        dishService.updateStatus(status,id);
        //清除缓存
        clearCatch("dish_*");
        return Result.success();
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品id")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> dishs = dishService.selectByCategoryId(categoryId);
        return Result.success(dishs);
    }

    /**
     * 清除redis缓存
     * @param patten
     */
    private void clearCatch(String patten){
        Set<String> keys = stringRedisTemplate.keys(patten);
        stringRedisTemplate.delete(keys);
    }
}
